/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.m4399.shortcut_test.core.content;


import android.content.LocusId;
import android.os.Build;

import com.m4399.shortcut_test.core.util.Preconditions;

import androidx.annotation.RequiresApi;


public final class LocusIdCompat {

    private final String mId;
    // Only guaranteed to be non-null on SDK_INT >= 29.
    private final LocusId mWrapped;

    /**
     * Construct a new LocusIdCompat with the specified id.
     *
     * @throws IllegalArgumentException if {@code id} is empty or {@code null}.
     */
    public LocusIdCompat( String id) {
        mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
        if (Build.VERSION.SDK_INT >= 29) {
            mWrapped = Api29Impl.create(id);
        } else {
            mWrapped = null;
        }
    }

    /**
     * Gets the canonical {@code id} associated with the locus.
     */
    
    public String getId() {
        return mId;
    }

    public LocusId toLocusId() {
        return mWrapped;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mId == null) ? 0 : mId.hashCode());
        return result;
    }

    @Override
    public boolean equals( final Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final LocusIdCompat other = (LocusIdCompat) obj;
        if (mId == null) {
            return other.mId == null;
        } else {
            return mId.equals(other.mId);
        }
    }

    
    @Override
    public String toString() {
        return "LocusIdCompat[" + getSanitizedId() + "]";
    }

    /**
     * @return {@link LocusId} object from this compat object.
     */
    


    /**
     * Returns an instance of LocusIdCompat from given {@link LocusId}.
     */
    


    
    private String getSanitizedId() {
        final int size = mId.length();
        return size + "_chars";
    }

    // Inner class required to avoid VFY errors during class init.

    private static class Api29Impl {

        /**
         * @return {@link LocusId} object from this compat object.
         */
        
        static LocusId create( final String id) {
            if (Build.VERSION.SDK_INT>=29){

                return new LocusId(id);
            }
            return null;
        }

        /**
         * @return {@code id} from the LocusId object.
         */
        
        @RequiresApi(api = Build.VERSION_CODES.Q)
        static String getId(final LocusId obj) {
            if (Build.VERSION.SDK_INT>=29){

                return obj.getId();
            }
            return null;
        }
    }
}
